#!/bin/bash
if [ "$1" = "armv7a" ]; then
	echo $1
elif [ "$1" = "arm64" ]; then
	echo $1
else
	echo $0 "[ armv7a ]"
	exit 1
fi

ANDROID_NDK=/Users/wangkai/android/android-ndk-r14b

ARCH=$1
BUILD_NAME=ffmpeg-${ARCH}
PREFIX=./build/output/${BUILD_NAME}
CUR_DIR=`pwd`

#------------------------------------------------------------
#				install standalone toolchain
#------------------------------------------------------------
TOOLCHAIN_PATH=$CUR_DIR/build/toolchain
SYSROOT=$TOOLCHAIN_PATH/sysroot
ANDROID_PLATFORM=android-19
CROSS_PREFIX=arm-linux-androideabi
GCC_VER=4.9
TOOLCHAIN_NAME=${CROSS_PREFIX}-${GCC_VER}
TOOLCHAIN_TOUCH="$TOOLCHAIN_PATH/touch"

mkdir -p $TOOLCHAIN_PATH

STEPRET=
if [ ! -f "$TOOLCHAIN_TOUCH" ]; then
	$ANDROID_NDK/build/tools/make-standalone-toolchain.sh \
		--verbose \
		--install-dir=$TOOLCHAIN_PATH \
		--platform=$ANDROID_PLATFORM \
		--toolchain=$TOOLCHAIN_NAME \
		--force
	touch $TOOLCHAIN_TOUCH
	STEPRET=1
else
	STEPRET=2
fi

if [ $STEPRET -eq 1 ]; then
	echo "-------------- make standalone toolchain success --------------"
elif [ $STEPRET -eq 2 ]; then
	echo "-------------- standalone toolchain exist --------------"
else
	echo "************** make standalone toolchain fail!! **************"
	exit 1
fi

function conf_compile {
	#------------------------------------------------------------
	#				configure ffmpeg
	#------------------------------------------------------------
	./configure \
					--prefix=$PREFIX \
					--target-os=android \
					--cross-prefix=$TOOLCHAIN_PATH/bin/arm-linux-androideabi- \
					--arch=arm \
					--sysroot=$PLATFORM \
					--enable-cross-compile \
					--cc=$TOOLCHAIN_PATH/bin/arm-linux-androideabi-gcc \
					--nm=$TOOLCHAIN_PATH/bin/arm-linux-androideabi-nm \
					--ar=$TOOLCHAIN_PATH/bin/arm-linux-androideabi-ar \
					--strip=$TOOLCHAIN_PATH/bin/arm-linux-androideabi-strip \
					--disable-shared \
					--disable-programs \
					--disable-ffmpeg \
					--disable-ffplay \
					--disable-ffprobe \
					--disable-ffserver \
					--disable-doc \
					--disable-symver \
					--disable-postproc \
					--enable-small \
					--enable-gpl \
					--enable-asm \
					--enable-jni \
					--enable-mediacodec \
					--enable-decoder=h264_mediacodec \
					--enable-hwaccel=h264_mediacodec \
					--enable-decoder=hevc_mediacodec \
					--enable-decoder=mpeg4_mediacodec \
					--enable-decoder=vp8_mediacodec \
					--enable-decoder=vp9_mediacodec \
					--enable-nonfree \
					--enable-version3 \
					--extra-cflags="--sysroot=${SYSROOT} -I${SYSROOT}/usr/include -fPIE -DANDROID -Wno-multichar -Os -fpic -march=armv7-a -mcpu=cortex-a8" \
					--extra-ldflags="-Wl,-rpath-link=-I${SYSROOT}/usr/lib -L${SYSROOT}/usr/lib -L${TOOLCHAIN_PATH}/lib,--fix-cortex-a8" \
					--extra-libs="-lgcc"
					
					
	#------------------------------------------------------------
	#				compile ffmpeg
	#------------------------------------------------------------
	make
	make install
}
#------------------------------------------------------------
#				link ffmpeg.so
#------------------------------------------------------------
function link_so {
	LIBDIR=${PREFIX}/lib
	$TOOLCHAIN_PATH/bin/arm-linux-androideabi-ld \
					-rpath-link=$SYSROOT/usr/lib \
					-L$SYSROOT/usr/lib \
					-soname libffmpeg.so \
					-shared \
					-nostdlib \
					-Bsymbolic \
					--whole-archive \
					--no-undefined \
					-o ${PREFIX}/libffmpeg.so \
					${LIBDIR}/libavcodec.a \
					${LIBDIR}/libavfilter.a \
					${LIBDIR}/libavformat.a \
					${LIBDIR}/libavutil.a \
					${LIBDIR}/libswresample.a \
					${LIBDIR}/libswscale.a \
					${LIBDIR}/libavdevice.a \
					-lc -lm -lz -ldl -llog \
					--dynamic-linker=/system/bin/linker ${TOOLCHAIN_PATH}/lib/gcc/arm-linux-androideabi/4.9.x/libgcc.a
}

if [ "$2" = "link" ]; then
	link_so
else
	conf_compile
fi